﻿function createNewPoint(latitude, longitude, detail, map, Is_viewOnLoad, displayText) {
    //declare co-ordinate(point) of needed location
    var point = new GLatLng(latitude, longitude);

    //create mark of this point
    var marker = new GMarker(point);
    map.addOverlay(marker);

    //declare summary content of this location
    var WINDOW_HTML = "<div style='width: 150px; padding-right: 10px'><a href='#'>" + displayText + " </a></div>"

    //declare event to display summary content
    GEvent.addListener(marker, "click", function ()
    { marker.openInfoWindowHtml(WINDOW_HTML); });
    
    if (Is_viewOnLoad)
        marker.openInfoWindowHtml(WINDOW_HTML);
}

function createMap(address, map, isMiddle, displayText) {
    var geocoder, subjectAddress;
    geocoder = new GClientGeocoder();
    geocoder.getLocations(address, function (response) {
        if (!response || response.Status.code != 200) {
            alert("Sorry, we were unable to geocode the address");
        }
        else {
            subjectAddress = {
                lat: response.Placemark[0].Point.coordinates[1],
                lon: response.Placemark[0].Point.coordinates[0],
                address: response.Placemark[0].address
            };

            createNewPoint(subjectAddress.lat, subjectAddress.lon, address, map, false, displayText);

            if (isMiddle == true)
                map.centerAndZoom(new GLatLng(subjectAddress.lat, subjectAddress.lon), 2);
        }
    });
}

function DrawMap(mapId, address, isMiddle, displayText) {
    var displayAddress = "";
    if (typeof (displayText) == "undefined")
        displayAddress = address;
    else
        displayAddress = displayText;
    //create new map 
    //with containner is a "div" named "map"
    var map = new GMap(document.getElementById(mapId));

    //add more controls following map
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    
    createMap(address, map, isMiddle, displayAddress);
}